//
//  XFDHomeWorkPlatfromCtrl.m
//  XFDHomeWorkPlatfrom_Example
//
//  Created by 朱国良 on 2017/11/2.
//  Copyright © 2017年 GL-ZHU. All rights reserved.
//

#import "XFDHomeWorkPlatfromCtrl.h"
#import "XFDNodeMenu.h"
#import "XFDHomeTableViewCellOne.h"
#import <Masonry/Masonry.h>
#import <XFDCommon/UIImage+Extension.h>
#import <XFDCommon/UIColor+Extension.h>
#import <XFDCommon/XFDNetManager.h>

#define HBCOLOR(value) [UIColor HB_ColorWithHexString:value]

@interface XFDHomeWorkPlatfromCtrl ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UITableViewCell *cell1;
@property(nonatomic,strong)UITableViewCell *cell2;
@property(nonatomic,strong)XFDHomeTableViewCellOne *cellOne;
@property(nonatomic,copy)clickMenu clickBlock;
@property(nonatomic,strong)NSArray *row0Btns;
@end

@implementation XFDHomeWorkPlatfromCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
 
//    self.topBarBackgroundColor = [UIColor whiteColor];
//    self.navigationBarSeperateView.backgroundColor = [UIColor HB_ColorWithHexString:@"#c5c5c5"];
//
    UIImageView *logo = [[UIImageView alloc] init];
    logo.image = [UIImage hb_imagePathWithName:@"work_logo_ewatt" bundle:@"XFDHomeWorkPlatfrom" targetClass:[self class]];
    logo.frame = CGRectMake(0, 0, 115, 20);
    self.navigationItem.titleView = logo;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    /// 获取任务和设备数据
    __weak typeof(self) weakSelf = self;
    [XFDNetManager netTokenManagerRequestWithMethod:(kXMHTTPMethodGET) WithAPI:@"user/home" WithParams:nil WithSuccessBlock:^(id response) {
        weakSelf.overdue = [self deviceString:response[@"task"][@"expired"]];
        weakSelf.commit = [self deviceString:response[@"task"][@"reporting"]];
        weakSelf.working = [self deviceString:response[@"task"][@"running"]];
        weakSelf.complete = [self deviceString:response[@"task"][@"completed"]];
        
        weakSelf.unmanned = [self deviceString:response[@"device"][@"uav"]];
        weakSelf.unmannedNumber = [self deviceString:response[@"device"][@"model"]];
        weakSelf.traceDivice = [self deviceString:response[@"device"][@"trackingDevice"]];
        weakSelf.load = [self deviceString:response[@"device"][@"load"]];
        weakSelf.battery = [self deviceString:response[@"device"][@"battery"]];
    } WithFailurBlock:^(NSError *error, NSString *tips) {
        
    } withHUB:NO];
}

- (NSString *)deviceString:(NSString *)key{
    return [NSString stringWithFormat:@"%@",key];
}

#pragma mark - 内部控制函数
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
- (UITableViewCell *)cell1{
    if(!_cell1){
        _cell1 = [[UITableViewCell alloc] init];
        _cell1.selectionStyle = UITableViewCellSelectionStyleNone;
        NSArray *titles = @[@"已逾期",@"待上报",@"作业中",@"已完成"];
        NSArray *titleColors = @[@"#fd8c48",@"#3eabf9",@"#62cb89",@"#8f8ef2"];
        CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, 150);
        __weak typeof(self) weakSelf = self;
        XFDNodeMenu *menu = [XFDNodeMenu nodeMenuTitle:@"任务" nodes:^(UIImageView *topLeftImg, NSArray<UIButton *> *btns) {
            weakSelf.row0Btns = btns;
            
            topLeftImg.image = [UIImage hb_imagePathWithName:@"work_task" bundle:@"XFDHomeWorkPlatfrom" targetClass:[self class]];
            for (int i = 0; i < btns.count; i++) {
                UIButton *btn = btns[i];
                [btn setTitle:@"0" forState:UIControlStateNormal];
                [btn setTitleColor:HBCOLOR(titleColors[i]) forState:UIControlStateNormal];
                [btn setTitleColor:HBCOLOR(titleColors[i]) forState:UIControlStateHighlighted];
            }
        } andTitles:titles frame:rect];
        [_cell1.contentView addSubview:menu];
    }
    return _cell1;
}
- (UITableViewCell *)cell2{
    if(!_cell2){
        _cell2 = [[UITableViewCell alloc] init];
        _cell2.selectionStyle = UITableViewCellSelectionStyleNone;
        NSArray *titles = @[@"实时监视",@"项目管理",@"作业任务",@"数据统计"];
        NSArray *btnImgs = @[@"work_function_spy",@"work_function_project",@"work_function_task",@"work_function_stastic"];
        CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, 150);
        XFDNodeMenu *menu = [XFDNodeMenu nodeMenuTitle:@"功能" nodes:^(UIImageView *topLeftImg, NSArray<UIButton *> *btns) {
            
            topLeftImg.image = [UIImage hb_imagePathWithName:@"work_function" bundle:@"XFDHomeWorkPlatfrom" targetClass:[self class]];
            for (int i = 0; i < btns.count; i++) {
                UIButton *btn = btns[i];
                [btn setImage:[UIImage hb_imagePathWithName:btnImgs[i] bundle:@"XFDHomeWorkPlatfrom" targetClass:[self class]] forState:UIControlStateNormal];
                [btn setImage:[UIImage hb_imagePathWithName:btnImgs[i] bundle:@"XFDHomeWorkPlatfrom" targetClass:[self class]] forState:UIControlStateHighlighted];
                btn.tag = 1000 + i;
                [btn addTarget:self action:@selector(click:) forControlEvents:(UIControlEventTouchUpInside)];
            }
        } andTitles:titles frame:rect];
        [_cell2.contentView addSubview:menu];
    }
    return _cell2;
}
- (XFDHomeTableViewCellOne *)cellOne{
    if(!_cellOne){
        _cellOne = [[XFDHomeTableViewCellOne alloc] init];
        _cellOne.selectionStyle = UITableViewCellSelectionStyleNone;
        __weak typeof(self) weakSelf = self;
        [_cellOne clickMenu:^(NSInteger index) {
            switch (index) {
                case 1:
                    weakSelf.clickBlock ?weakSelf.clickBlock(XFDHomeWorkPlatfromMenuUnmanned) : nil;
                break;
                case 2:
                    weakSelf.clickBlock ?weakSelf.clickBlock(XFDHomeWorkPlatfromMenuUnmannedNumber) : nil;
                break;
                case 3:
                    weakSelf.clickBlock ?weakSelf.clickBlock(XFDHomeWorkPlatfromMenuTraceDivice) : nil;
                break;
                case 4:
                    weakSelf.clickBlock ?weakSelf.clickBlock(XFDHomeWorkPlatfromMenuLoad) : nil;
                break;
                case 5:
                    weakSelf.clickBlock ?weakSelf.clickBlock(XFDHomeWorkPlatfromMenuBattery) : nil;
                break;
                default:
                break;
            }
        }];
    }
    return _cellOne;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return self.cell1;
    }else if (indexPath.row == 1){
       return self.cell2;
    }else if (indexPath.row == 2){
        return self.cellOne;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 2) return 256;
    return 150;
}
    
- (void)clickHomeMenu:(clickMenu)clickMenuBlock{
    self.clickBlock = clickMenuBlock;
}
    
- (void)click:(UIButton *)btn{
    switch (btn.tag) {
        case 1000:
            self.clickBlock ? self.clickBlock(XFDHomeWorkPlatfromMenuMonitor) : nil;
        break;
        case 1001:
            self.clickBlock ? self.clickBlock(XFDHomeWorkPlatfromMenuProgectManage) : nil;
        break;
        case 1002:
            self.clickBlock ? self.clickBlock(XFDHomeWorkPlatfromMenuWorkJob) : nil;
        break;
        case 1003:
            self.clickBlock ?self.clickBlock(XFDHomeWorkPlatfromMenuDataStatistics) : nil;
        break;
        default:
        break;
    }
}
    
- (void)setOverdue:(NSString *)overdue{
    _overdue = [overdue copy];
    UIButton *btn = self.row0Btns[0];
    [btn setTitle:_overdue forState:UIControlStateNormal];
}
- (void)setCommit:(NSString *)commit{
    _commit = [commit copy];
    UIButton *btn = self.row0Btns[1];
    [btn setTitle:_commit forState:UIControlStateNormal];
}
- (void)setWorking:(NSString *)working{
    _working = [working copy];
    UIButton *btn = self.row0Btns[2];
    [btn setTitle:_working forState:UIControlStateNormal];
}
- (void)setComplete:(NSString *)complete{
    _complete = [complete copy];
    UIButton *btn = self.row0Btns[3];
    [btn setTitle:_complete forState:UIControlStateNormal];
}
- (void)setUnmanned:(NSString *)unmanned{
    _unmanned = [unmanned copy];
    self.cellOne.label1.text = _unmanned;
}
- (void)setUnmannedNumber:(NSString *)unmannedNumber{
    _unmannedNumber = unmannedNumber;
    self.cellOne.label2.text = _unmannedNumber;
}
- (void)setTraceDivice:(NSString *)traceDivice{
    _traceDivice = traceDivice;
    self.cellOne.label3.text = _traceDivice;
}
- (void)setLoad:(NSString *)load{
    _load = [load copy];
    self.cellOne.label4.text = _load;
}
- (void)setBattery:(NSString *)battery{
    _battery = [battery copy];
    self.cellOne.label5.text = _battery;
}


    
@end
